package com.xiongmm.stream.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

/**
 * @author: XiongMM
 * @date: 2019/5/24 13:57
 * @description:
 */
public class BambooUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(BambooUtils.class);

    private static final Long ONE_SECOND = 1000L;

    /**
     * 获取unix时间
     *
     * @param date
     * @return
     */
    public static int getUnixTimeByDate(Date date) {
        return getUnixTimeByMillisTime(getMillisTimeByDate(date));
    }

    /**
     * 获取millis time
     *
     * @param date
     * @return
     */
    public static long getMillisTimeByDate(Date date) {
        return date.getTime();
    }

    /**
     * 获取millis time
     *
     * @param millisTime
     * @return
     */
    public static int getUnixTimeByMillisTime(long millisTime) {
        return (int) Math.ceil((double) millisTime / ONE_SECOND);
    }

    /**
     * 获取当前时间的unix
     *
     * @return
     */
    public static int getCurrUnixTime() {
        return (int) Math.ceil((double) System.currentTimeMillis() / ONE_SECOND);
    }

    /**
     * 返回但前的日期
     *
     * @return
     */
    public static Date getCurrDate() {
        return new Date();
    }

    /**
     * 获取当前millis time
     *
     * @return
     */
    public static long getCurrMillisTime() {
        return System.currentTimeMillis();
    }

    /**
     * unix time 转 millis time
     *
     * @param unixTime
     * @return
     */
    public static long getMillisTimeByUnixTime(int unixTime) {
        return unixTime * ONE_SECOND;
    }

    /**
     * unix time 转 millis time
     *
     * @param unixTime
     * @return
     */
    public static long getMillisTimeByUnixTime(long unixTime) {
        return unixTime * ONE_SECOND;
    }

    /**
     * millis time 转 date
     *
     * @param millisTime
     * @return
     */
    public static Date getDateFromMillisTime(long millisTime) {
        return new Date(millisTime);
    }

    /**
     * unix time 转 date
     *
     * @param collectTime
     * @return
     */
    public static Date getDateFromUnixTime(long collectTime) {
        return new Date(collectTime * ONE_SECOND);
    }

    /**
     * unix time to date
     *
     * @param unixTime
     * @return
     */
    public static Date unixTimeToDate(int unixTime) {
        return new Date(getMillisTimeByUnixTime(unixTime));
    }

    /**
     * 保留两位小数
     *
     * @param d
     * @return
     */
    public static double formatDouble2(double d) {
        BigDecimal bg = new BigDecimal(d).setScale(2, RoundingMode.UP);
        return bg.doubleValue();
    }

    /**
     * value转化为Long
     *
     * @param value
     * @return
     */
    public static Long objectToLong(Object value) {
        try {
            if(value == null){
                return 0L;
            }
            return Long.valueOf(String.valueOf(value));
        } catch (Exception ex) {
            LOGGER.error("BambooUtils.objectToLong() ----> value:{} 转化为Long，失败～～", value, ex);
        }
        return 0L;
    }

    /**
     * value 转化为Integer
     *
     * @param value
     * @return
     */
    public static Integer objectToInteger(Object value) {
        try {
            if(value == null){
                return 0;
            }
            return Integer.valueOf(String.valueOf(value));
        } catch (Exception ex) {
            LOGGER.error("BambooUtils.objectToLong() ----> value:{} 转化为Integer，失败～～", value, ex);
        }
        return 0;
    }

    public static void main(String[] args) {
        System.out.println("当前时间戳是：" + getCurrUnixTime());
        System.out.println("date:" + unixTimeToDate(1558677761));
        System.out.println(formatDouble2(1234.13348759099848747494));
    }
}
